package com.iceGame;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.RemoteInput;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;



public class GameScreen implements Screen{
	
	static final int GAME_READY = 0;
	static final int GAME_RUNNING = 1;
	static final int GAME_PAUSED = 2;
	static final int GAME_LEVEL_END = 3;
	static final int GAME_OVER = 4;
	static final int GAME_END = 5;
	
	int state;
	static PhysicsWorld world;
	WorldRenderer renderer;
	Box2DDebugRenderer debugrenderer;
	Game game;
	FPSLogger fps;
	public static OrthographicCamera guiCam;
	public static Vector3 touchPoint;
	Rectangle pauseBounds;
	Rectangle resumeBounds;
	Rectangle replayBounds;
	Rectangle quitBounds;
	Rectangle soundbounds;
	Rectangle GameOverBounds;
	int lastVolume;
	String VolumeString;
	SpriteBatch batcher; 
	float maxIceCubeSize; // = 100% in the life bar
	float minIceCubeSize; // = 0% in the life bar
	float currentIceCubeSize;
	
	public GameScreen(Game game, int level){
		this.world = new PhysicsWorld("level" + level);
		this.game = game;
		state = GAME_READY;
		this.renderer = new WorldRenderer(world);
		debugrenderer = new Box2DDebugRenderer();
		fps = new FPSLogger();
		/*if (Settings.musicEnabled)
			Assets.music.play();
		else
			Assets.music.stop();*/
		guiCam = new OrthographicCamera(800, 480);		
		guiCam.position.set(800 / 2, 480 / 2, 0);
		touchPoint = new Vector3();		
		soundbounds = new Rectangle(640,400,80,80);
		pauseBounds = new Rectangle(800 - 80, 480 - 80, 80, 80);		
		resumeBounds = new Rectangle(400 - 180/2, 240 - 60/2+80, 180, 53);
		replayBounds = new Rectangle(400 - 140/2, 240 - 60/2, 140, 60);
		GameOverBounds = new Rectangle(400 - 287/2, 240 - 60/2+80, 287, 51);
		quitBounds = new Rectangle(400 - 133/2, 240 - 60/2-80, 133, 62);
		lastVolume = 0;
		VolumeString = "Volume: 0";
		batcher = new SpriteBatch();
		//RemoteInput receiver = new RemoteInput();		
		//Gdx.input = receiver;
		minIceCubeSize = 0.16f;
		maxIceCubeSize = world.currentLevel.iceCube.bounds.height*world.currentLevel.iceCube.bounds.width-minIceCubeSize;		
		currentIceCubeSize  = maxIceCubeSize; 
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(float deltaTime) {
		currentIceCubeSize = world.currentLevel.iceCube.bounds.height*world.currentLevel.iceCube.bounds.width ;
		switch (state) {
		case GAME_READY:
			updateReady();
			break;
		case GAME_RUNNING:
			updateRunning(deltaTime);
			break;
		case GAME_PAUSED:
			updatePaused();
			break;
		case GAME_LEVEL_END:
			updateLevelEnd();
			break;
		case GAME_OVER:
			updateGameOver();
			break;
		case GAME_END:
			updateGameEnd();
			break;
		}
		//TODO- maybe to move somewhere else
		present(deltaTime);
	}

	private void updatePaused() {
		if (Gdx.input.justTouched()) {
			guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

			if (OverlapTester.pointInRectangle(resumeBounds, touchPoint.x, touchPoint.y)) {
				state = GAME_RUNNING;	
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
			/*	if (Settings.musicEnabled)
					Assets.music.play();
				else
					Assets.music.stop();*/
				return;
			}

			if (OverlapTester.pointInRectangle(quitBounds, touchPoint.x, touchPoint.y)) {
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				game.setScreen(new MainMenuScreen(game));
				Assets.music.stop();
				return;

			}
			
			if (OverlapTester.pointInRectangle(replayBounds, touchPoint.x, touchPoint.y)) {
				if (Settings.soundEnabled){
					Assets.buttonPressSound.play();
				}

				String nextLevel = "level" + world.currentLevel.levelNumber;
				this.world = new PhysicsWorld(nextLevel);
				this.renderer = new WorldRenderer(world);
				maxIceCubeSize = world.currentLevel.iceCube.bounds.height*world.currentLevel.iceCube.bounds.width-minIceCubeSize;
				currentIceCubeSize  = maxIceCubeSize; 
				/*if (Settings.musicEnabled)
					Assets.music.play();
				else
					Assets.music.stop();*/
				state = GAME_READY;
				return;

			}
		}
		
	}

	
	private void updateGameOver() {
		if(Gdx.input.justTouched()) {
		
			guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));


			if (OverlapTester.pointInRectangle(quitBounds, touchPoint.x, touchPoint.y)) {
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				game.setScreen(new MainMenuScreen(game));
				Assets.music.stop();
				return;
			}		
			if (OverlapTester.pointInRectangle(replayBounds, touchPoint.x, touchPoint.y)) {
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				String nextLevel = "level" + world.currentLevel.levelNumber;
				this.world = new PhysicsWorld(nextLevel);
				this.renderer = new WorldRenderer(world);
				maxIceCubeSize = world.currentLevel.iceCube.bounds.height*world.currentLevel.iceCube.bounds.width-minIceCubeSize;
				currentIceCubeSize  = maxIceCubeSize; 
				state = GAME_READY;
				return;

			}
		}
		
	}

	private void updateLevelEnd() {	
		
		if (Settings.soundEnabled){
			Assets.endLevelSound.play();
		}
		Assets.music.pause();
		int tScore = 1,score=0;
        if(world.score > world.currentLevel.threeCubesScore){
             tScore = 3;                     
        }
        else if(world.score > world.currentLevel.twoCubesScore){
             tScore = 2;
        }
        else{
             tScore = 1;
        }
        if(tScore > Settings.scores.get(world.currentLevel.levelNumber-1)){
            score=tScore;
        }
        else{
        	score = Settings.scores.get(world.currentLevel.levelNumber-1);
        }
        Settings.scores.set(world.currentLevel.levelNumber-1,score);     
        world.numOfStars = tScore;
		if( world.currentLevel.levelNumber<world.topLevel) {                         
            Settings.isLock.set(world.currentLevel.levelNumber,false);  
           // Assets.music.stop();
            game.setScreen(new ScoreMenuScreen(game,world));
            state = GAME_READY;
		}
	    else if (world.currentLevel.levelNumber>=world.topLevel){       
	    	game.setScreen(new ScoreMenuScreen(game,world));                
	    	//Assets.music.stop();
	    	state = GAME_END;
	       
	    }
		 Settings.save();
	}

	private void updateRunning(float deltaTime) {		
		// when pause has been pressed
		if (Gdx.input.justTouched()) {
			guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));			
			if (OverlapTester.pointInRectangle(pauseBounds, touchPoint.x, touchPoint.y)) {
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				//Assets.music.stop();
				state = GAME_PAUSED;
				return;
			}
			if (OverlapTester.pointInRectangle(soundbounds, touchPoint.x, touchPoint.y)) {
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				Settings.soundEnabled = !Settings.soundEnabled;
				return;
			}
		}
		if (world.state == PhysicsWorld.WORLD_STATE_NEXT_LEVEL){
			state = GAME_LEVEL_END;
		}
		if (world.state == PhysicsWorld.WORLD_STATE_GAME_OVER) {
			 if (Settings.soundEnabled){
				 	Assets.music.pause();
					
			 }
			 if (Settings.musicEnabled){
				 Assets.gameOverSound.play();
			 }
			// Assets.music.stop();
			state = GAME_OVER;
		}
		world.update(deltaTime);
		fps.log();
		
	}

	private void updateReady() {
		if (Gdx.input.justTouched()) {
			state = GAME_RUNNING;
			if (Settings.musicEnabled){
				Assets.music.play();
			}
			/*if (Settings.musicEnabled)
				Assets.music.play();
			else
				Assets.music.stop();*/
		}
		
		
	}
	
	private void updateGameEnd(){
		// TODO Auto-generated method stub
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}
	
	public void present (float deltaTime) {
		GLCommon gl = Gdx.gl;
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		renderer.render();
		//debugrenderer.render(this.world.world);
		

		guiCam.update();
		batcher.setProjectionMatrix(guiCam.combined);	
		batcher.enableBlending();
		batcher.begin();	
		presentBar();
		switch (state) {
		case GAME_READY:
			presentReady();
			break;
		case GAME_RUNNING:
			presentRunning();
			break;
		case GAME_PAUSED:
			presentPaused();
			break;
		case GAME_LEVEL_END:
			presentLevelEnd();
			break;
		case GAME_OVER:
			presentGameOver();
			break;
		case GAME_END:
			presentGameEnd();
			break;
		}
		batcher.end();				
	}

	private void presentGameOver() {
		batcher.draw(Assets.gameOverText, GameOverBounds.x, GameOverBounds.y,GameOverBounds.width,GameOverBounds.height);
		batcher.draw( Assets.retryText, replayBounds.x, replayBounds.y, replayBounds.width, replayBounds.height);
		batcher.draw( Assets.quitText, quitBounds.x, quitBounds.y, quitBounds.width, quitBounds.height);		
			
	}
	
	private void presentGameEnd() {
		// TODO Auto-generated method stub
		
	}

	private void presentLevelEnd() {
		// TODO Auto-generated method stub
		
	}

	private void presentPaused() {
		batcher.draw( Assets.resumeText, resumeBounds.x, resumeBounds.y, resumeBounds.width, resumeBounds.height);
		batcher.draw( Assets.retryText, replayBounds.x, replayBounds.y, replayBounds.width, replayBounds.height);
		batcher.draw( Assets.quitText, quitBounds.x, quitBounds.y, quitBounds.width, quitBounds.height);		
	}

	private void presentRunning() {
		
		if (Settings.soundEnabled){
			batcher.draw(Assets.mute, 640, 400, 80,80);
		}
		else{
			batcher.draw(Assets.unmute, 640, 400, 80,80);
		}
		batcher.draw(Assets.pause, 800 - 80 , 480 - 80, 80, 80);

	}

	private void presentReady() {
		batcher.draw(Assets.readyText, 400 - 223 / 2, 240 - 80 / 2, 205, 95);		
	}
	
	private void presentBar() {		
		float barSize = 93f;
		float ratio = (currentIceCubeSize-minIceCubeSize) / maxIceCubeSize ;
		if (ratio == 1){
			batcher.draw(Assets.backBar,22, 480-33.0f-11,barSize,22);
		}
		else if (ratio >1){ // Cube increases
			maxIceCubeSize = currentIceCubeSize;
			batcher.draw(Assets.backBar,22, 480-33.0f-11,barSize*ratio,22);
		}
		else {// cube reduces 
			batcher.draw(Assets.backBar,22, 480-33.0f-11,barSize*ratio,22);
		}
		batcher.draw(Assets.topBar, 10,  480-44.0f-10f,120,43);
	}



}